package com.wjj.happytime.adapter.listen_frg_adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.jiongbull.jlog.JLog;
import com.squareup.picasso.Picasso;
import com.wjj.happytime.R;
import com.wjj.happytime.activity.listen_activity.PlayActivity;
import com.wjj.happytime.adapter.listen_frg_adapter.NomalRadioAdapter.NomalRadioViewHolder;
import com.wjj.happytime.entity.listen_entity.HomePageRadioDetail;
import com.wjj.happytime.entity.listen_entity.RadioDetail;
import java.util.List;

/**
 * Created by junjie.wang on 2017/4/25.
 */

public class NomalRadioAdapter extends Adapter<NomalRadioViewHolder>{
  public Context context;
  public List<RadioDetail> datas;

  public List<HomePageRadioDetail> topRadios;//排行
  public LayoutInflater inflater;

  public int itemCount=0;

  public NomalRadioAdapter(Context context,
      List<RadioDetail> datas) {
    this.context = context;
    this.datas = datas;
    inflater = LayoutInflater.from(context);
  }

  //添加新数据
  public void addDatas(List<RadioDetail> newDatas){
    datas.addAll(newDatas);
    notifyDataSetChanged();
  }
  public void setTopRadios(List<HomePageRadioDetail> topRadios){
    this.topRadios=topRadios;
  }



  @Override
  public NomalRadioViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.frg_live_peritem, parent, false);
    NomalRadioViewHolder holder = new NomalRadioViewHolder(view);
    return holder;
  }

  @Override
  public void onBindViewHolder(NomalRadioViewHolder holder, int position) {
    if (datas!=null) {
      final RadioDetail radioDetail = datas.get(position);
      Picasso.with(context).load(radioDetail.getRadioCoverLarge()).into(holder.ivPic);
      holder.tvName.setText(radioDetail.getRname());
      holder.tvProgramName.setText(radioDetail.getProgramName());
      holder.tvPlayCount.setText(radioDetail.getRadioPlayCount()+"");
      holder.ivPlay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
          Intent intent = new Intent(context, PlayActivity.class);
          intent.putExtra("playUrl",radioDetail.getRadioPlayUrl().getRadio_64_ts());
          intent.putExtra("radioName",radioDetail.getRname());
          context.startActivity(intent);
        }
      });
    }
    if (topRadios!=null) {
      final HomePageRadioDetail radioDetail = topRadios.get(position);
      Picasso.with(context).load(radioDetail.getCoverLarge()).into(holder.ivPic);
      holder.tvName.setText(radioDetail.getName());
      holder.tvProgramName.setText(radioDetail.getProgramName());
      holder.tvPlayCount.setText(radioDetail.getPlayCount()+"");
      holder.ivPlay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
          Intent intent = new Intent(context, PlayActivity.class);
          intent.putExtra("playUrl",radioDetail.getPlayUrl().getTs64());
          intent.putExtra("radioName",radioDetail.getName());
          context.startActivity(intent);
        }
      });
    }

  }

  @Override
  public int getItemCount() {
    if (datas!=null) {
      itemCount= datas.size();
    }
    if (topRadios!=null) {
      itemCount= topRadios.size();
    }
    return itemCount;
  }

  public class NomalRadioViewHolder extends RecyclerView.ViewHolder{

    public ImageView ivPic;
    public TextView tvName;
    public TextView tvProgramName;
    public TextView tvPlayCount;
    public ImageView ivPlay;

    public NomalRadioViewHolder(View itemView) {
      super(itemView);
      ivPic = ((ImageView) itemView.findViewById(R.id.iv_peritem_id));
      tvName = ((TextView) itemView.findViewById(R.id.tv_peritem_radioname_id));
      tvProgramName = ((TextView) itemView.findViewById(R.id.tv_peritem_programName_id));
      tvPlayCount = ((TextView) itemView.findViewById(R.id.tv_peritem_playCount_id));
      ivPlay = ((ImageView) itemView.findViewById(R.id.iv_play));
    }
  }



}
